import { createApp, toRaw } from "vue";
import App from "./App.vue";

import mitt from "mitt";

import { createPinia, PiniaPluginContext } from "pinia";
import "./assets/css/reset.less";

//本地存储函数
const setLocal = (key: string, data: any) => {
  localStorage.setItem(key, JSON.stringify(data));
};

//本地提取
const getLocal = (key: string) => {
  return localStorage.getItem(key)
    ? JSON.parse(localStorage.getItem(key) as string)
    : {};
};

//pinia插件
const piniaPlugin = (option: any) => {
  return (context: PiniaPluginContext) => {
    const { store } = context;
    console.log(store);

    const data = getLocal(`${option?.key}-${store.$id}`);
    store.$subscribe((arg) => {
      setLocal(`${option?.key}-${arg.storeId}`, toRaw(store.$state));
    });

    return {
      ...data,
    };
  };
};

const pinia = createPinia();
pinia.use(
  piniaPlugin({
    key: "dzx",
  })
);
const app = createApp(App);

// app.config.globalProperties.$bus = Mitt
// app.config.globalProperties.$man = {
//   data(str:string){
//     return `我是${str}`
//   }
// }

app.use(pinia);
app.mount("#app");
